package emt.friendtracking.maps.org.ci.geo.route;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.util.Log;

public class Helper
{
	private static final String TAG = Helper.class.getSimpleName();
	public static InputStream getConnection(String url)
	{
		InputStream is = null;
		try
		{
			URLConnection conn = new URL(url).openConnection();
			is = conn.getInputStream();
		}
		catch (MalformedURLException e)
		{ e.printStackTrace(); }
		catch (IOException e)
		{ e.printStackTrace(); }
		
		return is;
	}
	
	public static double getDistance(double meLongitude, double meLatitude, double destLongitude, double destLatitude)
	{
		String url = RoadProvider.getUrl(meLatitude, meLongitude, destLatitude, destLongitude);
		InputStream is = Helper.getConnection(url);
		Road mRoad = RoadProvider.getRoute(is);
		Log.i(TAG, mRoad.mDescription);
		
		String[] description = mRoad.mDescription.split(" ");
		
		double distMeter;
		
		if (description[1].contains("k"))
			distMeter = Double.parseDouble(description[1].substring(0, description[1].length() - 2)) * 1000;
		else
			distMeter = Double.parseDouble(description[1].substring(0, description[1].length() - 1));
		
		return distMeter;
	}
}
